package com.docwei.thansgank.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import com.docwei.thansgank.R;
import com.docwei.thansgank.adapter.MyOsAdapter;
import com.docwei.thansgank.callback.ResultBean;
import com.docwei.thansgank.util.DensityUtil;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * Created by tobo on 17/2/3.
 */

public class AndroidFragment extends BaseFragement implements SwipeRefreshLayout.OnRefreshListener {

    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    public List<ResultBean.GankBean> mList = new ArrayList<>();
    private MyOsAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fuli, null);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler);
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe);
        initData();
        return view;

    }

    private void initData() {
        mSwipeRefreshLayout.setProgressViewOffset(false, 0, DensityUtil.dip2px(getActivity(), 24));
        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.teal, R.color.orange);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        mAdapter = new MyOsAdapter(mList, getActivity());
        mRecyclerView.setAdapter(mAdapter);
        mSwipeRefreshLayout.setRefreshing(true);
        mSwipeRefreshLayout.setOnRefreshListener(this);
    }


    @Override
    protected void pullData() {
        System.out.println("去发请求啦---android");
        String url = "http://gank.io/api/data/Android/40/1";
        OkHttpUtils
                .get()
                .url(url)
                .id(100)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onAfter(int id) {
                        mSwipeRefreshLayout.setRefreshing(false);
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        if (e.getMessage() == null) {
                            return;
                        }
                        Toast.makeText(getActivity(), e.getMessage().toString(), Toast.LENGTH_SHORT);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        ResultBean bean = gson.fromJson(response, ResultBean.class);
                        List<ResultBean.GankBean> results = bean.getResults();
                        mList.addAll(results);
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }

    @Override
    public void onRefresh() {
        pullData();
    }
}
